﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiveManager
{
    public partial class Supplier_Edit : Form
    {

        protected DiveDatabaseEntities diveDatabase;
        
        public string id_search;
        
        public string firma;
        public string firma_alt;
        public string kdnr_lieferant;
        public string kdnr_lieferant_alt;

        public string strasse;
        public string strasse_alt;
        public string hsnr;
        public string hsnr_alt;
        public string adr_zusatz;
        public string adr_zusatz_alt;
        public int plz;
        public int plz_alt;
        public string ort;
        public string ort_alt;
        public string land;
        public string land_alt;

        public string telefon1;
        public string telefon1_alt;
        public string telefon2;
        public string telefon2_alt;
        public string mobil1;
        public string mobil1_alt;
        public string mobil2;
        public string mobil2_alt;
        public string fax;
        public string fax_alt;
        public string email;
        public string email_alt;
        public string www;
        public string www_alt;

        public string bank;
        public string bank_alt;
        public string konto;
        public string konto_alt;
        public string blz;
        public string blz_alt;
        public string iban;
        public string iban_alt;
        public string bic;
        public string bic_alt;

        public string ust_id;
        public string ust_id_alt;
        public string steuernummer;
        public string steuernummer_alt;
        public string finanzamt;
        public string finanzamt_alt;
        public string reg_eintrag;
        public string reg_eintrag_alt;
        public string reg_sitz;
        public string reg_sitz_alt;

        public int edited =0;
        
        public Supplier_Edit()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
        }

        private void edit_Click(object sender, EventArgs e)
        {
           
        }

        private void Supplier_Edit_Load(object sender, EventArgs e)
        {
            //Übernahme der GUID aus Detailanzeige Lieferanten
            this.id_search = Weitergabe_Edit;
            

            //Ausgabe der Grunddaten aus der DB
            //ID ist String und wird in GUID convertiert
            Guid id_guid = new Guid(id_search);

            //Abfrage in Supplier_Main DB
            var query_main = from sm in diveDatabase.Supplier_Main
                             where sm.ID == id_guid
                             select sm;

            foreach (var sm in query_main)
            {
                string firma = sm.Firma;
                string kdnr = sm.KDNR_Lieferant;

                txSupplier.Text = firma;
                txKDNR.Text = kdnr;
            }

            //Abfrage in Supplier_Kommunkikation
            var query_kommunikation = from sk in diveDatabase.Supplier_Kommunication
                                      where sk.ID == id_guid
                                      select sk;

            foreach (var sk in query_kommunikation)
            {
                string telefon1 = sk.Telefon1;
                string telefon2 = sk.Telefon2;
                string fax = sk.Fax;
                string mobil1 = sk.Mobil1;
                string mobil2 = sk.Mobil2;
                string mail = sk.Email;
                string www = sk.WWW;

                txTelefon1.Text = telefon1;
                txTelefon2.Text = telefon2;
                txFax.Text = fax;
                txMobil1.Text = mobil1;
                txMobil2.Text = mobil2;
                txEmail.Text = mail;
                txWWW.Text = www;
            }

            //Abfrage in Supplier_Adress
            var query_adress = from sa in diveDatabase.Supplier_Adress
                               where sa.ID == id_guid
                               select sa;

            foreach (var sa in query_adress)
            {
                string strasse = sa.Strasse;
                string hsnr = sa.HSNR;
                string adrzu = sa.Adr_Zusatz;
                string plz = sa.PLZ.ToString();
                string ort = sa.Ort;
                string land = sa.Land;

                txStrasse.Text = strasse;
                txHSNR.Text = hsnr;
                txAdrZusatz.Text = adrzu;
                txPLZ.Text = plz;
                txOrt.Text = ort;
                txLand.Text = land;
            }

            //Abfrage in Supplier_Financial
            var query_financial = from sfi in diveDatabase.Supplier_Financial
                                  where sfi.ID == id_guid
                                  select sfi;

            foreach (var sfi in query_financial)
            {
                string bank = sfi.Bank;
                string konto = sfi.Konto;
                string blz = sfi.BLZ;
                string iban = sfi.IBAN;
                string bic = sfi.BIC;

                txBank.Text = bank;
                txKonto.Text = konto;
                txBLZ.Text = blz;
                txIBAN.Text = iban;
                txBIC.Text = bic;
            }

            //Abfrage in Supplier_Registration
            var query_registration = from sreg in diveDatabase.Supplier_Registration
                                     where sreg.ID == id_guid
                                     select sreg;

            foreach (var sreg in query_registration)
            {
                string ustid = sreg.UST_ID;
                string strnr = sreg.STNR;
                string finanzamt = sreg.Finanzamt;
                string regeintrag = sreg.Reg_Eintrag;
                string regsitz = sreg.Reg_Sitz;

                txUST.Text = ustid;
                txSteuerNr.Text = strnr;
                txFinanzamt.Text = finanzamt;
                txRegEintrag.Text = regeintrag;
                txRegSitz.Text = regsitz;
            }

            //Ausgangswerte in Variablen schreiben
            firma_alt = txSupplier.Text;
            kdnr_lieferant_alt = txKDNR.Text;
            strasse_alt = txStrasse.Text;
            hsnr_alt = txHSNR.Text;
            adr_zusatz_alt = txAdrZusatz.Text;
            ort_alt = txOrt.Text;
            land_alt = txLand.Text;
            telefon1_alt = txTelefon1.Text;
            telefon2_alt = txTelefon2.Text;
            mobil1_alt = txMobil1.Text;
            mobil2_alt = txMobil2.Text;
            fax_alt = txFax.Text;
            email_alt = txEmail.Text;
            www_alt = txWWW.Text;
            bank_alt = txBank.Text;
            konto_alt = txKonto.Text;
            blz_alt = txBLZ.Text;
            iban_alt = txIBAN.Text;
            bic_alt = txBIC.Text;
            ust_id_alt = txUST.Text;
            steuernummer_alt = txSteuerNr.Text;
            finanzamt_alt = txFinanzamt.Text;
            reg_eintrag_alt = txRegEintrag.Text;
            reg_sitz_alt = txRegSitz.Text;

        }

        private void exit_Click(object sender, EventArgs e)
        {
            //Form beenden
            this.Close();
        }

        public string Weitergabe_Edit
        {
            get;
            set;
        }

        private void edit_Click_1(object sender, EventArgs e)
        {
            //Änderungen übernehmen
            //TextBoxen auslesen und in Variablen speichern
            firma = txSupplier.Text;
            kdnr_lieferant = txKDNR.Text;
            strasse = txStrasse.Text;
            hsnr = txHSNR.Text;
            adr_zusatz = txAdrZusatz.Text;

            if (txPLZ.Text.Equals(""))
            {
                plz = 0;
            }
            else
            {
                plz = Convert.ToInt32(txPLZ.Text);
            }

            ort = txOrt.Text;
            land = txLand.Text;
            telefon1 = txTelefon1.Text;
            telefon2 = txTelefon2.Text;
            mobil1 = txMobil1.Text;
            mobil2 = txMobil2.Text;
            fax = txFax.Text;
            email = txEmail.Text;
            www = txWWW.Text;
            bank = txBank.Text;
            konto = txKonto.Text;
            blz = txBLZ.Text;
            iban = txIBAN.Text;
            bic = txBIC.Text;
            ust_id = txUST.Text;
            steuernummer = txSteuerNr.Text;
            finanzamt = txFinanzamt.Text;
            reg_eintrag = txRegEintrag.Text;
            reg_sitz = txRegSitz.Text;

            //Eingaben überprüfen
            if (txSupplier.Text.Equals(""))
            {
            //Messagebox mit Fehlermeldung
                MessageBox.Show("Bite Firmenname eingeben!");
            }
            else
            {
                if (txKDNR.Text.Equals(""))
                {
            //Messagebox mit Fehlermeldung
                    MessageBox.Show("Bitte Kundennummer beim Lieferanten eingeben!");
                }
                else
                {
            //Aus id_search eine GUID machen
            Guid id_guid = new Guid(id_search);

            //Schreiben in DB
            //Schreiben in Supplier_Main
            var edit_sm = from sm in diveDatabase.Supplier_Main
                          where sm.ID == id_guid
                          select sm;

            foreach (Supplier_Main s in edit_sm)
            {
                s.Firma = firma;
                s.KDNR_Lieferant = kdnr_lieferant;


            }

            //Schreiben in Supplier_Kommunacation
            var edit_sk = from sk in diveDatabase.Supplier_Kommunication
                          where sk.ID == id_guid
                          select sk;

            foreach (Supplier_Kommunication suk in edit_sk)
            {
                suk.Telefon1 = telefon1;
                suk.Telefon2 = telefon2;
                suk.Mobil1 = mobil1;
                suk.Mobil2 = mobil2;
                suk.Fax = fax;
                suk.Email = email;
                suk.WWW = www;
            }


            //Schreiben in Supplier_Adress
            var edit_sa = from sa in diveDatabase.Supplier_Adress
                          where sa.ID == id_guid
                          select sa;

            foreach (Supplier_Adress sua in edit_sa)
            {
                sua.Strasse = strasse;
                sua.HSNR = hsnr;
                sua.Adr_Zusatz = adr_zusatz;
                sua.PLZ = plz;
                sua.Ort = ort;
                sua.Land = land;
            }

            //Schreiben in Supllier_Financial
            var edit_sf = from sf in diveDatabase.Supplier_Financial
                          where sf.ID == id_guid
                          select sf;

                    foreach(Supplier_Financial suf in edit_sf)
                    {
                        suf.Bank = bank;
                        suf.Konto = konto;
                        suf.BLZ = blz;
                        suf.IBAN = iban;
                        suf.BIC = bic;
                    }


                    //Schreiben in Supplier_Registration
                    var edit_sr = from sr in diveDatabase.Supplier_Registration
                                  where sr.ID == id_guid
                                  select sr;

                    foreach(Supplier_Registration sur in edit_sr)
                    {
                        sur.UST_ID = ust_id;
                        sur.STNR = steuernummer;
                        sur.Finanzamt = finanzamt;
                        sur.Reg_Eintrag = reg_eintrag;
                        sur.Reg_Sitz = reg_sitz;
                    }

            diveDatabase.SaveChanges();


            //Erstellen einer GUID für den Historieneintrag
            Guid guid_history = System.Guid.NewGuid();
            //Überprüfen, was geändert wurde
            //Wenn etwas geändert wurde, Historienenträge erstellen

            if (firma_alt != firma)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Firma";
                newsupplier_HistoryDetails.Old = firma_alt;
                newsupplier_HistoryDetails.New = firma;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (kdnr_lieferant != kdnr_lieferant_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "KDNR beim Lieferanten";
                newsupplier_HistoryDetails.Old = kdnr_lieferant_alt;
                newsupplier_HistoryDetails.New = kdnr_lieferant;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (strasse != strasse_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Straßen";
                newsupplier_HistoryDetails.Old = strasse_alt;
                newsupplier_HistoryDetails.New = strasse;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (hsnr != hsnr_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Hausnummer";
                newsupplier_HistoryDetails.Old = hsnr_alt;
                newsupplier_HistoryDetails.New = hsnr;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (adr_zusatz != adr_zusatz_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Adresszusatz";
                newsupplier_HistoryDetails.Old = adr_zusatz_alt;
                newsupplier_HistoryDetails.New = adr_zusatz;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (plz != plz_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "PLZ";
                newsupplier_HistoryDetails.Old = plz_alt.ToString();
                newsupplier_HistoryDetails.New = plz.ToString();
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (ort != ort_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Ort";
                newsupplier_HistoryDetails.Old = ort_alt;
                newsupplier_HistoryDetails.New = ort;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (land != land_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Land";
                newsupplier_HistoryDetails.Old = land_alt;
                newsupplier_HistoryDetails.New = land;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (telefon1 != telefon1_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Telefon 1";
                newsupplier_HistoryDetails.Old = telefon1_alt;
                newsupplier_HistoryDetails.New = telefon1;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (telefon2 != telefon2_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Telefon 2";
                newsupplier_HistoryDetails.Old = telefon2_alt;
                newsupplier_HistoryDetails.New = telefon2;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (mobil1 != mobil1_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Mobil 1";
                newsupplier_HistoryDetails.Old = mobil1_alt;
                newsupplier_HistoryDetails.New = mobil1;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (mobil2 != mobil2_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Mobil 2";
                newsupplier_HistoryDetails.Old = mobil2_alt;
                newsupplier_HistoryDetails.New = mobil2;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (fax != fax_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Fax";
                newsupplier_HistoryDetails.Old = fax_alt;
                newsupplier_HistoryDetails.New = fax;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (email != email_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Email";
                newsupplier_HistoryDetails.Old = email_alt;
                newsupplier_HistoryDetails.New = email;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (www != www_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "WWW";
                newsupplier_HistoryDetails.Old = www_alt;
                newsupplier_HistoryDetails.New = www;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (bank != bank_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Bank";
                newsupplier_HistoryDetails.Old = bank_alt;
                newsupplier_HistoryDetails.New = bank;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (konto != konto_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Konto";
                newsupplier_HistoryDetails.Old = konto_alt;
                newsupplier_HistoryDetails.New = konto;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (blz != blz_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "BLZ";
                newsupplier_HistoryDetails.Old = blz_alt;
                newsupplier_HistoryDetails.New = blz;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (iban != iban_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "IBAN";
                newsupplier_HistoryDetails.Old = iban_alt;
                newsupplier_HistoryDetails.New = iban;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (bic != bic_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "BIC";
                newsupplier_HistoryDetails.Old = bic_alt;
                newsupplier_HistoryDetails.New = bic;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (ust_id != ust_id_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "UST ID";
                newsupplier_HistoryDetails.Old = ust_id_alt;
                newsupplier_HistoryDetails.New = ust_id;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (steuernummer != steuernummer_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Steuernummer";
                newsupplier_HistoryDetails.Old = steuernummer_alt;
                newsupplier_HistoryDetails.New = steuernummer;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (finanzamt != finanzamt_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Finanzamt";
                newsupplier_HistoryDetails.Old = finanzamt_alt;
                newsupplier_HistoryDetails.New = finanzamt;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (reg_eintrag != reg_eintrag_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Register Eintrag";
                newsupplier_HistoryDetails.Old = reg_eintrag_alt;
                newsupplier_HistoryDetails.New = reg_eintrag;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

            if (reg_sitz != reg_sitz_alt)
            {
                //Änderungsanzeiger auf 1 setzen
                edited = 1;
                //Erstellen einer GUID für den Historieneintrag
                Guid guid_detail = System.Guid.NewGuid();
                //In die Tabelle Supplier_History schreiben
                // Generate new Object for DB
                Supplier_HistoryDetails newsupplier_HistoryDetails = new Supplier_HistoryDetails();
                newsupplier_HistoryDetails.ID = guid_detail;
                newsupplier_HistoryDetails.History_ID = guid_history;
                newsupplier_HistoryDetails.Changed = "Register Sitz";
                newsupplier_HistoryDetails.Old = reg_sitz_alt;
                newsupplier_HistoryDetails.New = reg_sitz;
                // Add to DB
                diveDatabase.AddToSupplier_HistoryDetails(newsupplier_HistoryDetails);
                diveDatabase.SaveChanges();
            }

                    if(edited ==1)
                    {
                        //In die Tabelle Supplier_History schreiben
                        // Generate new Object for DB
                        Supplier_History newsupplier_History = new Supplier_History();
                        newsupplier_History.ID = guid_history;
                        newsupplier_History.Supplier_ID = id_guid;
                        newsupplier_History.Changes = "Lieferant wurde geändert";
                        newsupplier_History.User = "Funktioniert nicht";
                        // Add to DB
                        diveDatabase.AddToSupplier_History(newsupplier_History);
                        diveDatabase.SaveChanges();

                        //Eintrag ins Reporting System
                    }


            //Messagebox mit Meldung
            MessageBox.Show("Lieferant geändert");

            //Form schließen
            this.Close();







            }
            }
        }
    }
}
